# HUD.gd
extends CanvasLayer

@onready var health_bar: ProgressBar = $MarginContainer/VBoxContainer/StatsContainer/HealthBar
@onready var health_label: Label = $MarginContainer/VBoxContainer/StatsContainer/HealthLabel
@onready var experience_bar: ProgressBar = $MarginContainer/VBoxContainer/StatsContainer/ExperienceBar
@onready var level_label: Label = $MarginContainer/VBoxContainer/StatsContainer/LevelLabel
@onready var time_label: Label = $MarginContainer/VBoxContainer/TopBar/TimeLabel
@onready var score_label: Label = $MarginContainer/VBoxContainer/TopBar/ScoreLabel
@onready var weapon_container: HBoxContainer = $MarginContainer/VBoxContainer/WeaponContainer # Container for weapon icons

# Optional: References for player node if not using SignalBus exclusively
# var player: Player = null

func _ready():
	# Connect to SignalBus signals
	SignalBus.player_health_changed.connect(update_health)
	SignalBus.player_experience_changed.connect(update_experience)
	SignalBus.player_level_up.connect(update_level)
	SignalBus.score_updated.connect(update_score)
	SignalBus.time_updated.connect(update_time)
	SignalBus.weapon_added.connect(add_weapon_icon)
	SignalBus.weapon_upgraded.connect(update_weapon_icon) # Optional: show level/upgrade

	# Initialize display
	update_health(100.0, 100.0) # Default values
	update_experience(0.0, 10.0) # Default values
	update_level(1)
	update_score(0)
	update_time("00:00")
	print("HUD Ready")


func update_health(current: float, max_val: float):
	if health_bar:
		health_bar.max_value = max_val
		health_bar.value = current
	if health_label:
		health_label.text = "%d / %d" % [ceili(current), ceili(max_val)]


func update_experience(current: float, max_val: float):
	if experience_bar:
		experience_bar.max_value = max_val
		experience_bar.value = current


func update_level(level: int):
	if level_label:
		level_label.text = "Level: %d" % level


func update_score(score: int):
	if score_label:
		score_label.text = "Score: %d" % score


func update_time(time_string: String):
	if time_label:
		time_label.text = time_string


func add_weapon_icon(weapon_id: String):
	# Load icon based on weapon_id (assumes icons are named consistently, e.g., knife_icon.png)
	var icon_texture = load("res://assets/graphics/ui/icons/%s_icon.png" % weapon_id) # Adjust path
	if icon_texture:
		var icon_rect = TextureRect.new()
		icon_rect.texture = icon_texture
		icon_rect.name = weapon_id # Use weapon_id as node name for easy access
		icon_rect.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
		icon_rect.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
		icon_rect.custom_minimum_size = Vector2(32, 32) # Example size
		weapon_container.add_child(icon_rect)
	else:
		printerr("Could not load weapon icon for:", weapon_id)


func update_weapon_icon(weapon_id: String, _upgrade_details: String):
	# Optional: Find the icon and add a label or change appearance to show level/upgrade
	var icon_node = weapon_container.get_node_or_null(weapon_id)
	if icon_node:
		# Example: Add a small label for level, needs layout adjustments
		# var level_label = Label.new()
		# level_label.text = "Lv X" # Get actual level from player/weapon
		# icon_node.add_child(level_label)
		pass